package com.sanpu.kxe.business.login.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.login.contract.LoginConfig;
import com.sanpu.kxe.business.login.contract.SetPasswordContract;
import com.sanpu.kxe.business.login.presenter.SetPasswordPresenter;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.utils.SoftInputUtil;
import com.sanpu.kxe.utils.ToastUtil;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Description：忘记密码、修改密码
 * Created by 薛勇军 on 2018/6/14.
 */

public class SetPasswordActivity extends BaseTitleActivity implements SetPasswordContract.View {

    @Bind(R.id.phone_number)
    EditTextWithDel vPhoneNumber;
    @Bind(R.id.password)
    EditTextWithDel vPassword;
    @Bind(R.id.verify_pass)
    EditTextWithDel vVerifyPass;
    @Bind(R.id.check_code)
    EditTextWithDel vCheckCode;
    @Bind(R.id.send_code)
    TextView vSendCode;
    @Bind(R.id.next)
    RoundTextView vNext;

    private SetPasswordContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_set_password;
    }

    @Override
    protected void initView() {
        super.initView();
        String pwdType = getIntent().getStringExtra(LoginConfig.PWD_TYPE);
        mPresenter = new SetPasswordPresenter(this);
        mPresenter.initType(pwdType);
    }

    @Override
    protected void setListener() {
        super.setListener();
        RxView.clicks(vSendCode).throttleFirst(3, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                    String phoneNumber = vPhoneNumber.getText().toString();
                    if (TextUtils.isEmpty(phoneNumber)) {
                        ToastUtil.showTextViewPrompt("手机号码不能为空");
                        return;
                    }
                    mPresenter.sendCode(phoneNumber);
            }
        });
        vNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkParameter()) return;
                mPresenter.onNextClick(vPhoneNumber.getText().toString(),
                        vPassword.getText().toString(), vCheckCode.getText().toString());
            }
        });
    }

    @Override
    public void changeByStatus(boolean status) {
        if (status) {
            vSendCode.setEnabled(false);
            vSendCode.setTextColor(
                    ContextCompat.getColor(mContext, R.color.c_cccccc));
        } else {
            vSendCode.setEnabled(true);
            vSendCode.setText("获取验证码");//数据发送完后设置为原来的文字
            vSendCode.setTextColor(
                    ContextCompat.getColor(mContext, R.color.color_base));
        }
    }

    @Override
    public void byoyomi(Long aLong) {
        vSendCode.setText(aLong + "秒");
    }

    /**
     * 校验输入框的值
     */
    public boolean checkParameter() {
        if (TextUtils.isEmpty(vPhoneNumber.getText())) {
            ToastUtil.showTextViewPrompt("手机号码不能为空");
            SoftInputUtil.openKeybord(vPhoneNumber, mContext);
            return false;
        }
        if (TextUtils.isEmpty(vCheckCode.getText())) {
            ToastUtil.showTextViewPrompt("短信验证码不能为空");
            SoftInputUtil.openKeybord(vCheckCode, mContext);
            return false;
        }
        if (TextUtils.isEmpty(vPassword.getText())) {
            ToastUtil.showTextViewPrompt("密码不能为空");
            SoftInputUtil.openKeybord(vPassword, mContext);
            return false;
        }
        if (TextUtils.isEmpty(vVerifyPass.getText())) {
            ToastUtil.showTextViewPrompt("请再次输入密码");
            SoftInputUtil.openKeybord(vVerifyPass, mContext);
            return false;
        }
        if (vPassword.getText().length() < 6 || vVerifyPass.getText().length() < 6) {
            ToastUtil.showTextViewPrompt("密码不能小于6位数");
            return false;
        }
        if (!vPassword.getText().toString().equals(vVerifyPass.getText().toString())) {
            ToastUtil.showTextViewPrompt("两次密码不一致");
            SoftInputUtil.openKeybord(vVerifyPass, mContext);
            return false;
        }
        return true;
    }

    @Override
    public void resetPwdView(String accountName) {
        commonTitle.setTitle("重置密码");
        vPhoneNumber.setText(accountName);
        vPhoneNumber.setTextColor(ContextCompat.getColor(mContext, R.color.c_4a4a4a));
        vPhoneNumber.setEnabled(false);
        vPhoneNumber.hasIcon(false);
    }

    @Override
    public void forgetPwdView(String accountName) {
        commonTitle.setTitle("忘记密码");
        vPhoneNumber.setText(accountName);
    }

    @Override
    public void payPwdView(String accountName) {
        commonTitle.setTitle("修改支付密码");
        vPhoneNumber.setTextColor(ContextCompat.getColor(mContext, R.color.c_4a4a4a));
        vPhoneNumber.setText(accountName);
        vPhoneNumber.setEnabled(false);
        vPhoneNumber.hasIcon(false);

        InputFilter[] filter = new InputFilter[]{new InputFilter.LengthFilter(6)};
        vVerifyPass.setFilters(filter);
        vPassword.setFilters(filter);

        vPassword.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
        vVerifyPass.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
    }

    @Override
    public void setPresenter(SetPasswordContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected void onPause() {
        super.onPause();
        SoftInputUtil.hideSoftInput(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
